 /* 
 * Created on Jul 28, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.ba360.struts.action; 

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.ba360.portal.common.PortalConstants;
import com.ba360.portal.database.DBConnectionImpl;
import com.ba360.portal.database.dbo.User;
import com.ba360.portal.database.dbo.User_23Feb09;
import com.ba360.portal.util.EmailSender;
import com.ba360.portal.util.Log;
import com.ba360.struts.form.ResponseForm;


/**
 * @author ROnat
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class AdResponseAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionForward af = new ActionForward();
		af.setContextRelative(true);
		af.setRedirect(true);
		af.setName("Cancel");
		af.setPath("/portal/index.do");
		String action = request.getParameter(PortalConstants.ACTION);
		String cancel = request.getParameter("org.apache.struts.taglib.html.CANCEL");
	
		HttpSession session = request.getSession();
		ResponseForm rf = (ResponseForm)form;
		//System.out.println(rf.getRespEmail());
		//System.out.println(rf.getAdTitle());
		
		if (cancel != null && cancel.equals("Cancel")) {

			return af;
		}
		if (action != null && action.equals("Response")) {
			Properties prop = new Properties();
			//User user = (User)session.getAttribute(PortalConstants.USER);  Modified by Arun on 10-MAY-2009
			User_23Feb09 user = (User_23Feb09)session.getAttribute(PortalConstants.USER);
			int adId = Integer.parseInt(request.getParameter("adId"));
			prop.setProperty("FullName", getAdUserName(adId));
			prop.setProperty("FromResponseAddress", rf.getRespEmail());
			prop.setProperty("ADResponse",rf.getRespMessage());
			prop.setProperty("AdTitle",request.getParameter("AdTitle") );
			prop.setProperty("ToEmailAddr", getAdUserEmailAddr(adId));
			prop.setProperty("ADID",adId+"");
			Log.init(null);
			Log.info("Statting response");
			
			try{
				//saveResponse(adId,user.getUserId(),request.getParameter("AdTitle"),rf.getRespMessage());
				EmailSender.sendEmail(EmailSender.TYPE_ADRESPONSE,prop);
				Log.info("Send response");
			}catch (Exception e)
			{
				System.out.println(e);
				Log.error(e.toString());
			}
			
			prop = new Properties();
			prop.setProperty("FullName", user.getFN());//+" "+user.getLN());
			prop.setProperty("ADResponse", rf.getRespMessage());
			prop.setProperty("AdTitle", request.getParameter("AdTitle"));
			prop.setProperty("ToEmailAddr",user.getEmail());
			prop.setProperty("ADID",adId+"");
			try{
				EmailSender.sendEmail(EmailSender.TYPE_COPYADRESPONSE,prop);
			}catch (Exception e)
			{
				System.out.println(e);
			}
			
			return mapping.findForward("success");
		} else if (action != null && action.equals("Complaint")) {
			Properties prop = new Properties();
			User user = (User) session.getAttribute(PortalConstants.USER);
			int adId = Integer.parseInt(request.getParameter("adId"));
			prop.setProperty("FromComplaintAddress", rf.getRespEmail());
			prop.setProperty("subject", "Adv Complaint");
			prop.setProperty("ADComplaint", rf.getRespMessage());
			prop.setProperty("AdTitle", request.getParameter("AdTitle"));
			prop.setProperty("ToEmailAddr", "response@bangalore360.com");
			prop.setProperty("ADID", adId + "");
			Log.init(null);
			Log.info("Statting response");

			try {
				// saveResponse(adId,user.getUserId(),request.getParameter("AdTitle"),rf.getRespMessage());
				EmailSender.sendEmail(EmailSender.TYPE_COMPLAINT, prop);
				Log.info("Send complaint");
			} catch (Exception e) {
				System.out.println(e);
				Log.error(e.toString());
			}
			
			return mapping.findForward("success");
		}
		return mapping.findForward("failure");
	}
	
	public String getAdUserEmailAddr(int adId) throws Exception
	{
		DBConnectionImpl dbConn = new DBConnectionImpl();
		Connection con = dbConn.getConnection();
		Statement stmt = con.createStatement();
		
		ResultSet rs = stmt.executeQuery("select member_id,Contact_email from ads where ad_id="+adId);
		rs.next();
		String email = rs.getString("Contact_email");
		int memId = rs.getInt("member_id");
		rs.close();
		if (email==null || email.trim().length()==0)
		{
			rs = stmt.executeQuery("select emailaddress from userinfo where UserID="+memId);
			rs.next();
			email = rs.getString("emailaddress");
			rs.close();
		}
		stmt.close();
		con.close();
		return email;
	}
	
	public String getAdUserName(int adId) throws Exception
	{
		DBConnectionImpl dbConn = new DBConnectionImpl();
		Connection con = dbConn.getConnection();
		Statement stmt = con.createStatement();
		
		ResultSet rs = stmt.executeQuery("select member_id,Contact_email from ads where ad_id="+adId);
		rs.next();
		int memId = rs.getInt("member_id");
		rs.close();
		
			rs = stmt.executeQuery("select FirstName,LastName from userinfo where UserID="+memId);
			rs.next();
			String fn = rs.getString("FirstName");
			//fn = fn+" "+rs.getString("LastName");
			rs.close();
		
		stmt.close();
		con.close();
		return fn;
	}
	
	private void saveResponse(int adid, double userid, String adTitle, String response) throws Exception
	{
		DBConnectionImpl dbConn = new DBConnectionImpl();
		Connection con = dbConn.getConnection();
		Statement stmt = con.createStatement();
		
		stmt.execute("insert into responses (userid,adid,adTitle,message) values("+
				userid+","+adid+",'"+adTitle+"','"+response+"')");
	}
}